package com.yyb.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.yyb.cache.CacheService;
import com.yyb.domain.SysDictData;
import com.yyb.dto.SysDictDataAddDTO;
import com.yyb.dto.SysDictDataUpdateDTO;
import com.yyb.mapper.SysDictDataMapper;
import com.yyb.service.SysDictDataService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 
 * 
 * 
 */
@Service
public class SysDictDataServiceImpl extends BaseServiceImpl<SysDictData> implements SysDictDataService {

    @Resource
    private SysDictDataMapper sysDictDataMapper;

    @Resource
    private CacheService cacheService;
    /**
     * 添加字典数据
     *
     * @param sysDictDataAddDTO
     * @return
     */
    @Override
    public int insert(SysDictDataAddDTO sysDictDataAddDTO) {
        this.cacheService.clear();
        //创建SysDictData
        SysDictData sysDictData=new SysDictData();
        BeanUtils.copyProperties(sysDictDataAddDTO,sysDictData);
        //填充其它数据
        sysDictData.setCreateBy(this.getUsername());
        DateTime date = DateUtil.date();
        sysDictData.setCreateTime(date);
        sysDictData.setUpdateTime(DateUtil.date());
        return this.sysDictDataMapper.insert(sysDictData);
    }

    /**
     * 根据ID查询字典数据
     *
     * @param dictCode
     * @return
     */
    @Override
    public SysDictData selectByPrimaryKey(Long dictCode) {
        return this.sysDictDataMapper.selectByPrimaryKey(dictCode);
    }

    /**
     * 修改字典数据
     *
     * @param sysDictDataUpdateDTO
     * @return
     */
    @Override
    public int updateByPrimaryKey(SysDictDataUpdateDTO sysDictDataUpdateDTO) {
        cacheService.clear();
        //创建SysDictData
        SysDictData sysDictData=new SysDictData();
        BeanUtils.copyProperties(sysDictDataUpdateDTO,sysDictData);
        //填充其它数据
        sysDictData.setUpdateBy(this.getUsername());
        sysDictData.setUpdateTime(DateUtil.date());
        return this.sysDictDataMapper.updateByPrimaryKeySelective(sysDictData);
    }

    /**
     * 删除字典数据
     *
     * @param dictCode
     * @return
     */
    @Override
    public int deleteByPrimaryKey(Long dictCode) {
        cacheService.clear();
        return this.sysDictDataMapper.deleteByPrimaryKey(dictCode);
    }

    /**
     * 根据字典类型获取字典数据
     * 从先缓存里面取，没有就走数据库
     *
     *
     * @param dictType
     * @return
     */
    @Override
    public List<SysDictData> getDictDataByDictType(String dictType) {
        //先查询缓存
        List<SysDictData> sysDictDataList=cacheService.getDictDataByDictType(dictType);
        if(null!=sysDictDataList){
            return sysDictDataList;
        }
        //说明缓存里面没有数据，要查询数据库查询
        List<SysDictData> dictDataByDictTypeList = this.sysDictDataMapper.getDictDataByDictType(dictType);
        //放到缓存里面取
        cacheService.setDictDataByDictType(dictType,dictDataByDictTypeList);
        return dictDataByDictTypeList;
    }
}
